package com.loc;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.os.Messenger;
import android.os.Process;
import android.text.TextUtils;
import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.services.geocoder.GeocodeSearch;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/* compiled from: ApsManager.java */
/* loaded from: classes2.dex */
public final class e {
    static boolean g = false;
    Context e;
    private List<Messenger> w;
    private boolean o = false;
    private boolean p = false;
    String a = null;
    b b = null;
    private long q = 0;
    private long r = 0;
    private ew s = null;
    AMapLocation c = null;
    private long t = 0;
    private int u = 0;
    a d = null;
    private j v = null;
    er f = null;
    HashMap<Messenger, Long> h = new HashMap<>();
    fz i = null;
    long j = 0;
    long k = 0;
    private long x = 0;
    String l = null;
    private boolean y = true;
    private String z = "";
    AMapLocationClientOption m = null;
    AMapLocationClientOption n = new AMapLocationClientOption();

    /* compiled from: ApsManager.java */
    /* loaded from: classes2.dex */
    public class a extends Handler {
        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        public a(Looper looper) {
            super(looper);
            e.this = r1;
        }

        /* JADX WARN: Removed duplicated region for block: B:127:0x011d A[Catch: all -> 0x012b, TryCatch #2 {all -> 0x012b, blocks: (B:92:0x0058, B:95:0x005e, B:128:0x0127, B:97:0x0063, B:98:0x006b, B:99:0x0072, B:100:0x007b, B:101:0x0080, B:103:0x0084, B:105:0x008c, B:107:0x0098, B:108:0x00a1, B:110:0x00a9, B:112:0x00b5, B:113:0x00bd, B:115:0x00c1, B:117:0x00c9, B:119:0x00d5, B:121:0x00ea, B:122:0x00f0, B:123:0x00f6, B:124:0x00fc, B:125:0x0107, B:126:0x0112, B:127:0x011d, B:91:0x0051), top: B:137:0x0051 }] */
        /* JADX WARN: Removed duplicated region for block: B:94:0x005c  */
        @Override // android.os.Handler
        /*
            Code decompiled incorrectly, please refer to instructions dump.
            To view partially-correct code enable 'Show inconsistent code' option in preferences
        */
        public final void handleMessage(android.os.Message r9) {
            /*
                Method dump skipped, instructions count: 330
                To view this dump change 'Code comments level' option to 'DEBUG'
            */
            throw new UnsupportedOperationException("Method not decompiled: com.loc.e.a.handleMessage(android.os.Message):void");
        }
    }

    /* compiled from: ApsManager.java */
    /* loaded from: classes2.dex */
    public class b extends HandlerThread {
        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        public b(String str) {
            super(str);
            e.this = r1;
        }

        @Override // android.os.HandlerThread
        protected final void onLooperPrepared() {
            try {
                e.this.v = new j(e.this.e);
                fs.b(e.this.e);
                fs.a(e.this.e);
                e.this.f = new er(false);
                super.onLooperPrepared();
            }
        }

        @Override // android.os.HandlerThread, java.lang.Thread, java.lang.Runnable
        public final void run() {
            try {
                super.run();
            } catch (Throwable th) {
                ft.a(th, "APSManager$ActionThread", "run");
            }
        }
    }

    public e(Context context) {
        this.e = null;
        this.e = context;
    }

    private static ew a(int i, String str) {
        try {
            ew ewVar = new ew("");
            ewVar.setErrorCode(i);
            ewVar.setLocationDetail(str);
            return ewVar;
        } catch (Throwable th) {
            ft.a(th, "ApsServiceCore", "newInstanceAMapLoc");
            return null;
        }
    }

    public void a(Bundle bundle) {
        try {
            if (this.o) {
                er erVar = this.f;
                if (erVar != null) {
                    erVar.a();
                    return;
                }
                return;
            }
            ft.a(this.e);
            if (bundle != null) {
                this.n = ft.a(bundle.getBundle("optBundle"));
            }
            this.f.a(this.e);
            this.f.b();
            a(this.n);
            this.f.c();
            this.o = true;
            this.y = true;
            this.z = "";
            List<Messenger> list = this.w;
            if (list == null || list.size() <= 0) {
                return;
            }
            e();
        } catch (Throwable th) {
            this.y = false;
            th.printStackTrace();
            this.z = th.getMessage();
            ft.a(th, "ApsServiceCore", "init");
        }
    }

    public void a(Messenger messenger) {
        this.h.remove(messenger);
    }

    private static void a(Messenger messenger, int i, Bundle bundle) {
        if (messenger != null) {
            try {
                Message obtain = Message.obtain();
                obtain.setData(bundle);
                obtain.what = i;
                messenger.send(obtain);
            } catch (Throwable th) {
                ft.a(th, "ApsServiceCore", "sendMessage");
            }
        }
    }

    public void a(Messenger messenger, Bundle bundle) {
        if (bundle != null) {
            try {
                if (bundle.isEmpty() || this.p) {
                    return;
                }
                this.p = true;
                b(messenger);
            } catch (Throwable th) {
                ft.a(th, "ApsServiceCore", "doInitAuth");
            }
        }
    }

    public void a(Messenger messenger, AMapLocation aMapLocation, String str, eq eqVar) {
        Bundle bundle = new Bundle();
        bundle.setClassLoader(AMapLocation.class.getClassLoader());
        bundle.putParcelable("loc", aMapLocation);
        bundle.putString("nb", str);
        bundle.putParcelable("statics", eqVar);
        this.h.put(messenger, Long.valueOf(gb.b()));
        a(messenger, 1, bundle);
    }

    private void a(Messenger messenger, String str) {
        Bundle bundle = new Bundle();
        bundle.setClassLoader(AMapLocation.class.getClassLoader());
        bundle.putInt("I_MAX_GEO_DIS", fs.i() * 3);
        bundle.putInt("I_MIN_GEO_DIS", fs.i());
        bundle.putParcelable("loc", this.c);
        a(messenger, "COARSE_LOC".equals(str) ? 103 : 6, bundle);
    }

    private void a(AMapLocationClientOption aMapLocationClientOption) {
        try {
            er erVar = this.f;
            if (erVar != null) {
                erVar.a(aMapLocationClientOption);
            }
            if (aMapLocationClientOption != null) {
                g = aMapLocationClientOption.isKillProcess();
                if (this.m != null) {
                    if (aMapLocationClientOption.isOffset() != this.m.isOffset() || aMapLocationClientOption.isNeedAddress() != this.m.isNeedAddress() || aMapLocationClientOption.isLocationCacheEnable() != this.m.isLocationCacheEnable() || this.m.getGeoLanguage() != aMapLocationClientOption.getGeoLanguage()) {
                        this.r = 0L;
                    }
                    if (aMapLocationClientOption.isOffset() != this.m.isOffset() || this.m.getGeoLanguage() != aMapLocationClientOption.getGeoLanguage()) {
                        this.c = null;
                    }
                }
                this.m = aMapLocationClientOption;
            }
        } catch (Throwable th) {
            ft.a(th, "ApsServiceCore", "setExtra");
        }
    }

    private static AMapLocationClientOption b(Bundle bundle) {
        AMapLocationClientOption aMapLocationClientOption = null;
        try {
            aMapLocationClientOption = ft.a(bundle.getBundle("optBundle"));
            String string = bundle.getString("d");
            if (!TextUtils.isEmpty(string)) {
                o.a(string);
            }
        } catch (Throwable th) {
            ft.a(th, "APSManager", "parseBundle");
        }
        return aMapLocationClientOption;
    }

    static /* synthetic */ ew b(String str) {
        return a(10, str);
    }

    private void b(Messenger messenger) {
        try {
            this.f.f();
            if (fs.l()) {
                Bundle bundle = new Bundle();
                bundle.putBoolean("installMockApp", true);
                a(messenger, 9, bundle);
            }
        } catch (Throwable th) {
            ft.a(th, "ApsServiceCore", "initAuth");
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:115:0x0127 A[Catch: all -> 0x016d, TryCatch #2 {all -> 0x016d, blocks: (B:74:0x0008, B:77:0x0010, B:79:0x0029, B:81:0x002f, B:84:0x0047, B:86:0x004c, B:88:0x0079, B:90:0x0086, B:92:0x008f, B:94:0x00a0, B:113:0x011f, B:115:0x0127, B:116:0x012d, B:118:0x0131, B:119:0x013c, B:121:0x0140, B:132:0x0169, B:124:0x014f, B:126:0x0155, B:128:0x0159, B:96:0x00a9, B:98:0x00b9, B:101:0x00c3, B:103:0x00cb, B:108:0x00eb, B:104:0x00d3, B:106:0x00dc, B:107:0x00e4), top: B:137:0x0008 }] */
    /* JADX WARN: Removed duplicated region for block: B:118:0x0131 A[Catch: all -> 0x016d, TryCatch #2 {all -> 0x016d, blocks: (B:74:0x0008, B:77:0x0010, B:79:0x0029, B:81:0x002f, B:84:0x0047, B:86:0x004c, B:88:0x0079, B:90:0x0086, B:92:0x008f, B:94:0x00a0, B:113:0x011f, B:115:0x0127, B:116:0x012d, B:118:0x0131, B:119:0x013c, B:121:0x0140, B:132:0x0169, B:124:0x014f, B:126:0x0155, B:128:0x0159, B:96:0x00a9, B:98:0x00b9, B:101:0x00c3, B:103:0x00cb, B:108:0x00eb, B:104:0x00d3, B:106:0x00dc, B:107:0x00e4), top: B:137:0x0008 }] */
    /* JADX WARN: Removed duplicated region for block: B:121:0x0140 A[Catch: all -> 0x016d, TRY_LEAVE, TryCatch #2 {all -> 0x016d, blocks: (B:74:0x0008, B:77:0x0010, B:79:0x0029, B:81:0x002f, B:84:0x0047, B:86:0x004c, B:88:0x0079, B:90:0x0086, B:92:0x008f, B:94:0x00a0, B:113:0x011f, B:115:0x0127, B:116:0x012d, B:118:0x0131, B:119:0x013c, B:121:0x0140, B:132:0x0169, B:124:0x014f, B:126:0x0155, B:128:0x0159, B:96:0x00a9, B:98:0x00b9, B:101:0x00c3, B:103:0x00cb, B:108:0x00eb, B:104:0x00d3, B:106:0x00dc, B:107:0x00e4), top: B:137:0x0008 }] */
    /* JADX WARN: Removed duplicated region for block: B:123:0x014e  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct code enable 'Show inconsistent code' option in preferences
    */
    public void b(android.os.Messenger r12, android.os.Bundle r13) {
        /*
            Method dump skipped, instructions count: 372
            To view this dump change 'Code comments level' option to 'DEBUG'
        */
        throw new UnsupportedOperationException("Method not decompiled: com.loc.e.b(android.os.Messenger, android.os.Bundle):void");
    }

    public static void d() {
        g = false;
    }

    public void e() {
        if (gb.m(this.e)) {
            fv.a();
            return;
        }
        try {
            er erVar = this.f;
            if (erVar == null || erVar == null) {
                return;
            }
            erVar.a(this.d);
            this.f.g();
        } catch (Throwable th) {
            ft.a(th, "ApsServiceCore", "startColl");
        }
    }

    public void f() {
        try {
            fs.c(this.e);
        } catch (Throwable th) {
            ft.a(th, "ApsServiceCore", "doCallOtherSer");
        }
    }

    public final void a() {
        try {
            this.i = new fz();
            b bVar = new b("amapLocCoreThread");
            this.b = bVar;
            bVar.setPriority(5);
            this.b.start();
            this.d = new a(this.b.getLooper());
            this.w = new ArrayList();
        } catch (Throwable th) {
            ft.a(th, "ApsServiceCore", "onCreate");
        }
    }

    public final void a(Intent intent) {
        a aVar;
        if (!"true".equals(intent.getStringExtra("as")) || (aVar = this.d) == null) {
            return;
        }
        aVar.sendEmptyMessageDelayed(9, 100L);
    }

    final void a(Messenger messenger, Bundle bundle, String str) {
        AMapLocationClientOption b2;
        float f;
        if (bundle != null) {
            try {
                if (bundle.isEmpty()) {
                    return;
                }
                double d = bundle.getDouble("lat");
                double d2 = bundle.getDouble("lon");
                float f2 = bundle.getFloat("radius");
                long j = bundle.getLong("time");
                if ("FINE_LOC".equals(str)) {
                    AMapLocation aMapLocation = new AMapLocation(GeocodeSearch.GPS);
                    aMapLocation.setLatitude(d);
                    aMapLocation.setLocationType(1);
                    aMapLocation.setLongitude(d2);
                    aMapLocation.setAccuracy(f2);
                    aMapLocation.setTime(j);
                    this.f.a(aMapLocation);
                }
                if (fs.h() && (b2 = b(bundle)) != null && b2.isNeedAddress()) {
                    a(b2);
                    AMapLocation aMapLocation2 = this.c;
                    if (aMapLocation2 != null) {
                        f = gb.a(new double[]{d, d2, aMapLocation2.getLatitude(), this.c.getLongitude()});
                        if (f < fs.i() * 3) {
                            a(messenger, str);
                        }
                    } else {
                        f = -1.0f;
                    }
                    if (f == -1.0f || (f > fs.i() && gb.b() - this.x > fs.j() * 1000)) {
                        a(bundle);
                        this.c = this.f.a(d, d2);
                        this.x = gb.b();
                        AMapLocation aMapLocation3 = this.c;
                        if (aMapLocation3 == null || TextUtils.isEmpty(aMapLocation3.getAdCode())) {
                            return;
                        }
                        a(messenger, str);
                    }
                }
            } catch (Throwable th) {
                ft.a(th, "ApsServiceCore", "doLocationGeo");
            }
        }
    }

    public final void a(boolean z) {
        er erVar = this.f;
        if (erVar != null) {
            erVar.a(z);
        }
    }

    public final boolean a(String str) {
        if (TextUtils.isEmpty(this.l)) {
            this.l = ft.b(this.e);
        }
        return !TextUtils.isEmpty(str) && str.equals(this.l);
    }

    public final Handler b() {
        return this.d;
    }

    public final void b(Intent intent) {
        String stringExtra = intent.getStringExtra("a");
        if (!TextUtils.isEmpty(stringExtra)) {
            m.a(this.e, stringExtra);
        }
        String stringExtra2 = intent.getStringExtra("b");
        this.a = stringExtra2;
        l.a(stringExtra2);
        String stringExtra3 = intent.getStringExtra("d");
        if (TextUtils.isEmpty(stringExtra3)) {
            return;
        }
        o.a(stringExtra3);
    }

    public final void c() {
        try {
            HashMap<Messenger, Long> hashMap = this.h;
            if (hashMap != null) {
                hashMap.clear();
                this.h = null;
            }
            List<Messenger> list = this.w;
            if (list != null) {
                list.clear();
            }
            j jVar = this.v;
            if (jVar != null) {
                jVar.c();
                this.v = null;
            }
            this.o = false;
            this.p = false;
            this.f.e();
            a aVar = this.d;
            if (aVar != null) {
                aVar.removeCallbacksAndMessages(null);
            }
            this.d = null;
            if (this.b != null) {
                if (Build.VERSION.SDK_INT >= 18) {
                    fx.a(this.b, HandlerThread.class, "quitSafely", new Object[0]);
                } else {
                    this.b.quit();
                }
            }
            this.b = null;
            if (this.i != null && this.j != 0 && this.k != 0) {
                long b2 = gb.b() - this.j;
                fz.a(this.e, this.i.c(this.e), this.i.d(this.e), this.k, b2);
                this.i.e(this.e);
            }
            fz.a(this.e);
            av.b();
            if (g) {
                Process.killProcess(Process.myPid());
            }
        } catch (Throwable th) {
            ft.a(th, "apm", "tdest");
        }
    }
}
